var homeJs=null;
$(function () {
    homeJs=new HomeJs();
});
(function(){
    var HomeJs = window.HomeJs = function(){
        return new HomeJs.prototype.init();

    };
    HomeJs.fn = HomeJs.prototype = {
        urls:{
            // search: CommonUtils.siteName+"document/document/listDocumentWithPage?size=5",
            statNewLZCount: CommonUtils.siteName+"lzgd/baseInfo/statNewLZCount",
            // countBorrowByStatus: CommonUtils.siteName+"archives/borrow/countBorrowByStatus",
            statNewLZCountByCity: CommonUtils.siteName+"lzgd/baseInfo/statNewLZCountByCity",
            getWZGDWUrl:CommonUtils.siteName+"lzgd/baseInfo/statUnLZCountByCityLevel?size=5",
            // statBorrowByYear: CommonUtils.siteName+"archives/borrow/bCountYear",
            // statReturnByYear: CommonUtils.siteName+"archives/borrow/rCountYear",
        },

        init:function () {
            this.initOpt();
            this.initEvent();
            this.initWZGDWList();
            // this.initUnitDocList();
            this.initNewLZCount();
            // this.initCountBorrow();
            this.statNewLZCountByCityCharts();
            // this.statBorrowDocumentCharts();
            return this;
        },
        initOpt:function () {
            var that=this;

        },
        initEvent:function () {
            var that=this;
            $("#J_btnMorePersonDocList,#J_btnMoreUnitDocList").on("click",function () {
                var title = $(this).data("title");
                var url = $(this).attr("data-url");
                var param = $(this).attr("data-param");
                window.parent.AddTabs(url,title,param);
            })
            $(".widget-small").on("click",function () {
                var title = $(this).data("title");
                var url = $(this).attr("data-url");
                window.parent.AddTabs(url,title);
            })
        },

        /**
         * 未整改到位清单
         */
        initWZGDWList:function () {
            var that=this;
            layui.use('table', function() {
                var table = layui.table;
                var url=that.urls.getWZGDWUrl+"&cityLevel='xsq'"
                table.render({
                    elem: '#J_WZGDWList'
                    ,url:url,
                     page: {
                      limit: 5  // 每页显示10条数据
                    }
                    ,cols: [
                        [
                            {type:'numbers',title:'序号',width:80}
                            ,{field:'xsq', title:'县（市，区）', width:130}
                            ,{field:'total', title:'未整改数', width:130,style:'text-align=center'}
                            // ,{field:'total', title:'未整改数', width:150,templet: function(item){
                            //     var createDate=item.createDate;
                            //     return new Date(createDate).pattern("yyyy-MM-dd");
                            // }
                            // }
                        ]
                    ]
                    ,id:'tableReload'
                    ,page: true
                    ,response: {
                        statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
                    }
                    ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                        return {
                            "code": res.code, //解析接口状态
                            "msg": res.msg, //解析提示文本
                            "count": res.data.length, //解析数据长度
                            "data": res.data //解析数据列表
                        };
                    }
                });
                table.on('row(J_WZGDWList)', function(obj){
                    var data = obj.data;
                    var xsq=data.xsq;
                    var title = "未整改清单";
                    var url = CommonUtils.siteName+"/lzgd/baseInfo/index";
                    var param = "sfzgdw=否&xsq="+xsq;
                    window.parent.AddTabs(url,title,param);
                    obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
                });

            })
        },
        /**
         * 初始化不动产单位列表
         */
        initUnitDocList:function () {
            var that=this;
            layui.use('table', function() {
                var unitTable = layui.table;
                var url=that.urls.search+"&qlrType=2"
                unitTable.render({
                    elem: '#J_homeUnitDocList'
                    ,url:url
                    ,cols: [
                        [
                            {type:'numbers',title:'序号',width:80}
                            ,{field:'qzNum', title:'权利人证件号', width:130}
                            ,{field:'createDate', title:'创建时间', width:150,templet: function(item){
                                var createDate=item.createDate;
                                return new Date(createDate).pattern("yyyy-MM-dd");
                            }
                        }
                        ]
                    ]
                    ,id:'tableReload'
                    ,page: true
                    ,response: {
                        statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
                    }
                    ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                        return {
                            "code": res.code, //解析接口状态
                            "msg": res.msg, //解析提示文本
                            "count": res.data.totalElements, //解析数据长度
                            "data": res.data.content //解析数据列表
                        };
                    }
                });
                unitTable.on('row(J_homeUnitDocList)', function(obj){
                    var data = obj.data;
                    var url ='/document/document/basicDetail/'+data.id;
                    window.layerIndex = layer.open({
                        type: 2,
                        title: data.qzNum+"基本信息",
                        shadeClose: true,
                        maxmin: true,
                        area: ['700px','550px'],
                        content: [url, 'on']
                    });
                    //标注选中样式
                    obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
                });


            })
        },

        /**
         * 统计档案数量
         */
        initNewLZCount:function () {
            var that=this;
            $.ajax({
                url:that.urls.statNewLZCount,
                type: 'get',
                data:{},
                dataType: 'json',
                async: false,
                success: function (result) {
                    if(result.code===200){
                        $("#J_newLZCount").html(result.data);
                    }
                }
            });
            //当年新增
            $.ajax({
                url:that.urls.statNewLZCount,
                type: 'get',
                data:{qrxzYear:"2023年"},
                dataType: 'json',
                async: false,
                success: function (result) {
                    if(result.code===200){
                        $("#J_newLZCountCurrentYear").html(result.data);
                    }
                }
            });
            //已整改到位
            $.ajax({
                url:that.urls.statNewLZCount,
                type: 'get',
                data:{sfzgdw:"是"},
                dataType: 'json',
                async: false,
                success: function (result) {
                    if(result.code===200){
                        $("#J_yjZgdwCount").html(result.data);
                    }
                }
            });
            //未整改到位
            $.ajax({
                url:that.urls.statNewLZCount,
                type: 'get',
                data:{sfzgdw:"否"},
                dataType: 'json',
                async: false,
                success: function (result) {
                    if(result.code===200){
                        $("#J_unZgdwCount").html(result.data);
                    }
                }
            });
        },

        /**
         * 档案归档统计
         */
        statNewLZCountByCityCharts:function () {
            var that=this;
            $.ajax({
                url:that.urls.statNewLZCountByCity,
                type: 'get',
                data:{},
                dataType: 'json',
                async: false,
                success: function (result) {
                    var xdatas=[],totalDatas=[],zgdwDatas=[],wzgdwDatas=[];
                    if(result.code===200){
                        var data=result.data;
                        var length=data.length;
                        for(var i=0;i<length;i++){
                            var sqs=data[i].sqs;
                            xdatas.push(sqs);
                            var total=data[i].total;
                            totalDatas.push(total);
                            var zgdwCount=data[i].zgdwCount;
                            zgdwDatas.push(zgdwCount);
                            var wzgdwCount=data[i].wzgdwCount;
                            wzgdwDatas.push(wzgdwCount);
                        }
                        that.showDocStatResultLine(xdatas,totalDatas,zgdwDatas,wzgdwDatas);
                    }
                }
            });
        },

        /**
         * 显示档案柱状统计结果
         * @param xdatas
         * @param persondatas
         * @param unitdatas
         */
        showDocStatResultBar:function (xdatas,persondatas,unitdatas) {
            var option = {
                color: ['#006699', '#4cabce'],
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                        type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: [
                    {
                        type: 'category',
                        data: xdatas,
                        axisTick: {
                            alignWithLabel: true
                        }
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: '个人档案',
                        barGap: 0,
                        type: 'bar',
                        data:persondatas
                    },
                    {
                        name: '单位档案',
                        type: 'bar',
                        data: unitdatas
                    }
                ]
            };
            var myChart = echarts.init(document.getElementById('J_statDocument'));
            myChart.clear();
            myChart.setOption(option);
            myChart.resize();
        },

        /**
         * 显示档案线状统计结果
         * @param xdatas
         * @param persondatas
         * @param unitdatas
         */
        showDocStatResultLine:function (xdatas,totalDatas,zgdwDatas,wzgdwDatas) {
            var option = {
                color: ['#4cabfe', '#1362ce', '#d50c14'],
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                        type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                    }
                },
                legend: {
                    data: ['新增总数','已整改数',"未整改数"]
                },
                grid: {
                    containLabel: true
                },
                xAxis: [
                    {
                        type: 'category',
                        data: xdatas,
                        axisTick: {
                            alignWithLabel: true
                        },
                        axisLabel: {
                            color: '#A1A7B3',
                            interval: 0, // 横轴信息全部显示
                            rotate: -30, // -30度角倾斜显示
                        },
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: '新增总数',
                        barGap: 0,
                        type: 'bar',
                        data:totalDatas
                    },
                    {
                        name: '已整改数',
                        type: 'bar',
                        data: zgdwDatas
                    }
                    ,
                    {
                        name: '未整改数',
                        type: 'bar',
                        data: wzgdwDatas
                    }
                ]
            };
            var myChart = echarts.init(document.getElementById('J_statNewLZ'));
            myChart.clear();
            myChart.setOption(option);
            myChart.resize();
        },

        /**
         * 统计借阅和归还
         */
        statBorrowDocumentCharts:function(){
            var that=this;
            let form = {
                BxArrYear:[],
                RxArrYear:[],
                ByArrYear:[],
                RyArrYear:[],

            }
            $.ajax({
                type:'get',
                url:that.urls.statBorrowByYear,
                async:false,
                success:function (data) {
                    if (data.code === 200){
                        let result = data.data
                        result.forEach(item => {
                            form.BxArrYear.push(item.year)
                            form.ByArrYear.push(item.yearCount)
                        })
                    }
                }
            })

            $.ajax({
                type:'get',
                url:that.urls.statReturnByYear,
                async:false,
                success:function (data) {
                    if (data.code === 200){
                        let result = data.data
                        result.forEach(item => {
                            form.RxArrYear.push(item.year)
                            form.RyArrYear.push(item.yearCount)
                        })
                    }
                }
            })
            let BYear = echarts.init(document.getElementById('J_statBorrowDocument'))
            let optionYear1 = {
                title: {
                    text: '年度借阅归还统计'
                },
                tooltip: {},
                legend: {
                    data: ['借阅','归还']
                },
                xAxis: {
                    data: form.BxArrYear.length>form.RxArrYear.length? form.BxArrYear:form.RxArrYear
                },
                yAxis: {
                    type: 'value'
                },
                series: [{
                    name: '借阅',
                    type: 'bar', //柱状
                    data: form.ByArrYear,
                    itemStyle: {
                        normal: { //柱子颜色
                            color: '#55AA77'
                        }
                    },
                }
                    ,{
                        name:'归还',
                        type:'bar',
                        data:form.RyArrYear,
                        itemStyle:{
                            normal:{
                                color:'#C43C72'
                            }
                        }
                    }
                ],
                toolbox:{
                    feature: {
                        magicType: {
                            type: ['line', 'bar']
                        }
                    }
                }
            };
            let optionYear = {
                color: ['#D5866A', '#4cabce'],
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                        type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                    }
                },
                legend: {
                    data: ['借阅','归还']
                },
                grid: {
                    containLabel: true
                },
                xAxis: [
                    {
                        type: 'category',
                        data: form.BxArrYear.length>form.RxArrYear.length? form.BxArrYear:form.RxArrYear,
                        axisTick: {
                            alignWithLabel: true
                        }
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: '借阅',
                        barGap: 0,
                        type: 'bar',
                        data:form.ByArrYear
                    },
                    {
                        name: '归还',
                        type: 'bar',
                        data: form.RyArrYear
                    }
                ]
            };
            BYear.setOption(optionYear, true);
        }


    };
    HomeJs.fn.init.prototype = HomeJs.fn;
})();